package com.dyit.job;

import com.dyit.dao.IUserDao;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.List;

@Slf4j
public class CheckUsernameJob extends QuartzJobBean {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private IUserDao iUserDao;
    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        if(!stringRedisTemplate.keys("usernames").isEmpty()){
            log.debug("there is no names in redis....");
            stringRedisTemplate.delete("usernames");
        }
        List<String> list = iUserDao.findAllUserName();
        log.debug("find all users size:----->"+list.size());
        if(!list.isEmpty()){
            String[] strings = list.toArray(new String[list.size()]);
            stringRedisTemplate.opsForSet().add("usernames",strings);
        }

    }
}
